package com.cellulam.framework.scaffold.generate.project;

import com.cellulam.framework.scaffold.generate.utils.GenUtils;
import org.apache.commons.lang3.StringUtils;

public class MavenModuleInfo {

    private String projectName;
    private String moduleSubPackage;
    private String withinPackage;
    protected MavenProjectInfo projectInfo;

    public MavenModuleInfo(String projectName, MavenProjectInfo projectInfo) {
        this(projectName, projectName, projectInfo);
    }

    public MavenModuleInfo(String projectName, String moduleSubPackage, MavenProjectInfo projectInfo) {
        this.projectName = projectName;
        this.moduleSubPackage = moduleSubPackage;
        this.projectInfo = projectInfo;
    }

    /** edm-core */
    public String getModuleName() {
        return projectInfo._rootArtifactId + "-" + projectName;
    }

    /** ${top_dir}/${module_name} */
    public String getModulePath() {
        return projectInfo.rootDirectory + "/" + getModuleName();
    }

    /** com.test13.core */
    public String getPackage() {
        return getPackage(null);
    }

    public String getPackage(String subPackage) {
        String p = projectInfo._package;
        if (StringUtils.isNotBlank(moduleSubPackage)) {
            p += "." + moduleSubPackage;
        }
        if (StringUtils.isNotBlank(withinPackage)) {
            p += "." + withinPackage;
        }
        if (StringUtils.isNotBlank(subPackage)) {
            p += "." + subPackage;
        }
        return p;
    }

    public MavenModuleInfo copyWithPackage(String withinPackage) {
        MavenModuleInfo module = new MavenModuleInfo(projectName, moduleSubPackage, projectInfo);
        module.withinPackage = withinPackage;
        return module;
    }

    public String packageInPath() {
        return GenUtils.dotToSlash(getPackage());
    }

    /** ${top_dir}/${module_name}/src/main/java */
    public String javaPath() {
        return getModulePath() + "/" + "src/main/java";
    }

    /** ${top_dir}/${module_name}/src/main/resources */
    public String resourcesPath() {
        return resourcesPath(null);
    }

    public String resourcesPath(String subResourceDir) {
        return getModulePath() + "/" + "src/main/resources" + (StringUtils.isNotBlank(subResourceDir) ? "/" + subResourceDir : "");
    }

    public String fullPackagePath() {
        return javaPath() + "/" + packageInPath();
    }

    public String fullPath(String subPath) {
        return getModulePath() + "/" + subPath;
    }

    public String entityPackage(String dbName) {
        return StringUtils.isNotEmpty(dbName) ? this.getPackage("entity." + dbName) : this.getPackage("entity");
    }

    public String entityPackageInPath(String dbName) {
        return GenUtils.dotToSlash(entityPackage(dbName));
    }


}
